package com.hxzy.controller;

import cn.hutool.json.JSONUtil;
import com.hxzy.common.vo.R;
import com.hxzy.es.entity.EsSpuGoods;
import com.hxzy.es.util.JsonToBeanUtil;
import com.hxzy.product.entity.Sku;
import com.hxzy.product.vo.SpuVO;
import com.hxzy.remote.ProductServiceOpenFeignApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * ES产品服务
 *
 * @author tonneyyy
 */
@RestController
public class EsGoodsController {

    @Autowired
    private ProductServiceOpenFeignApi  productServiceOpenFeignApi;

    /**
     * 远程查询产品信息，组装成EsSpuGoods,  (向ES中新增数据一会再做)
     * @param spuid
     * @return
     */
    @GetMapping(value = "/goods/{id}")
    public R  remoteById(@PathVariable(value = "id") String spuid){
        //远程查询
        R spuById = this.productServiceOpenFeignApi.findSpuById(spuid);
        //转换成对象
        SpuVO spuVO = JsonToBeanUtil.toObject(spuById, SpuVO.class);
        //远程查询
        R<List<Sku>> skusBySpuId = this.productServiceOpenFeignApi.findSkusBySpuId(spuid);
        //转换成自己的对象
        List<Sku> skuList = JsonToBeanUtil.toList(skusBySpuId, Sku.class);

        //组成 EsSpuGoods
        EsSpuGoods  esSpuGoods=new EsSpuGoods();
        esSpuGoods.setId(spuid);
        esSpuGoods.setBrandId(spuVO.getBrandId());
        esSpuGoods.setBrandName(spuVO.getBrandName());
        esSpuGoods.setCaption(spuVO.getCaption());
 
        esSpuGoods.setCategory1Id(spuVO.getCategory1Id());
        esSpuGoods.setCategory1Name(spuVO.getCategory1Name());
        esSpuGoods.setCategory2Id(spuVO.getCategory2Id());
        esSpuGoods.setCategory2Name(spuVO.getCategory2Name());
        esSpuGoods.setCategory3Id(spuVO.getCategory3Id());
        esSpuGoods.setCategory3Name(spuVO.getCategory3Name());
        esSpuGoods.setName(spuVO.getName());
        
        //价格
        List<Integer> priceList = skuList.stream().map(p -> p.getPrice()).collect(Collectors.toList());
        esSpuGoods.setPrice(priceList);

        //得到模型规则  tb_spu中的  `spec_items的值
         esSpuGoods.setSkus( spuVO.getSpecItems());

       //各到产品的值 tb_sku的`spec   Map<String, Object> specs

        // {"颜色":["紫色"],"尺码":["250度","200度","100度","150度","300度"]}    转换成java对象
        String jsonStr=esSpuGoods.getSkus();
        Map map = JSONUtil.toBean(jsonStr, Map.class);
        esSpuGoods.setSpecs(map);

        //搜索 all
        String keyWord= esSpuGoods.getName() +" " + (Objects.isNull(esSpuGoods.getBrandName())?"":esSpuGoods.getBrandName())+ " " + spuVO.getCategory1Name()+" "+spuVO.getCategory2Name() + " "+spuVO.getCategory3Name();
        esSpuGoods.setAll(keyWord);

        //生成es索引的时间
        esSpuGoods.setCreateTime(new Date());

        //向es库中插入数据


        return R.okHasData(esSpuGoods);
    }

}
